#!/usr/bin/env bash

if [ $# -le 0 ];then
    echo "$0 output_dir"
    exit 1
fi

export GOPRIVATE=gitlab.yunzhen-data.com
export GOINSECURE=gitlab.yunzhen-data.com
export GOPATH=${GOPATH-"${HOME}/go"}
git config --global --replace-all url."git@gitlab.yunzhen-data.com:".insteadOf http://gitlab.yunzhen-data.com/
git config --global --add url."git@gitlab.yunzhen-data.com:".insteadOf https://gitlab.yunzhen-data.com/

output_dir=$1
echo "output_dir $output_dir"

self_name=$(readlink -f $0)
root_path=$(readlink -f $(dirname ${self_name}))

echo "Install dependencies"
make deps
if [ $? != 0 ];then
    echo "Install dependencies failed"
    exit 1
fi

echo "Build"
make
if [ $? != 0 ];then
    echo "Build failed"
    exit 1
fi
make debug
if [ $? != 0 ];then
    echo "Build failed"
    exit 1
fi

echo "Copy to output directory"
cp -rpv ${root_path}/bin ${output_dir}
if [ $? != 0 ];then
    echo "Copy bin failed "
    exit 1
fi
cp -rpv ${root_path}/ad4/start.py ${output_dir}
if [ $? != 0 ];then
    echo "Copy start.py failed "
    exit 1
fi
cp -rpv ${GOPATH}/bin/dlv ${output_dir}/bin/
if [ $? != 0 ];then
    echo "Copy dlv failed "
    exit 1
fi

echo "Build and Install completed"
